package com.putao.album.fileupload;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.putao.album.application.GlobalApplication;
import com.putao.album.util.Loger;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by jidongdong on 15/5/8.
 */
public class FileUploadManager {
    private Context mContext;
    private final String cmd = "command";
    static FileUploadManager mfileUploadManager;

    /**
     * 需要上传的文件列表
     */
    private LinkedList<FileInfo> mFilelist = new LinkedList<FileInfo>();
    private boolean forceNoWifiUpload;

    public static FileUploadManager getIntance() {
        if (mfileUploadManager == null) {
            return new FileUploadManager(GlobalApplication.getInstance());
        }
        return mfileUploadManager;
    }


    public FileUploadManager(Context context) {
        mContext = context;
    }

    /**
     * 添加上传文件
     *
     * @param filelist
     */
    public void setFiles(LinkedList<FileInfo> filelist) {
        if (filelist == null || filelist.size() == 0) {
            return;
        }
        mFilelist.clear();
        mFilelist.addAll(filelist);
    }

    /**
     * 设置即使打开了仅WIFI上传无WIFI也可以上传
     *
     * @param forceNoWifiUpload
     */
    public void setForceNoWifiUpload(boolean forceNoWifiUpload) {
        this.forceNoWifiUpload = forceNoWifiUpload;
    }
    /*
    * 用来判断服务是否运行.
    * @param context
    * @param className 判断的服务名字
    * @return true 在运行 false 不在运行
    */
    public boolean isServiceRunning(Context mContext,String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager)
                mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList
                = activityManager.getRunningServices(30);
        if (!(serviceList.size()>0)) {
            return false;
        }
        for (int i=0; i<serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                isRunning = true;
                break;
            }
        }
        //Loger.d("chen++++isRunning="+isRunning);
        return isRunning;
    }

    /**
     * 上传当前宝宝,未上传成功的图片
     * @param babyId
     */
    public void startUploadCurrentChildUnfinished(String babyId) {
        if (isServiceRunning(GlobalApplication.getInstance(), FileUploadService.class.getName())) {
            return;
        }
        List<FileInfo> files= FileUploadDBHelper.getInstance().getUnfinishedFiles(babyId);

        if(files!=null&&files.size()>0){
            LinkedList<FileInfo> list = new LinkedList<FileInfo>();
            for(int i=0;i<files.size();i++){
                list.add(files.get(i));
            }
            setFiles(list);
            setForceNoWifiUpload(false);
            start();
        }
    }

    /**
     * 开始上传
     */
    public void start() {
        if (mFilelist == null || mFilelist.size() == 0) {
            Loger.d("文件列表为空");
            return;
        }
        saveFileListToDb(mFilelist);
        Bundle bundle = new Bundle();
        bundle.putSerializable("filelist", mFilelist);
        bundle.putBoolean("forceNoWifiUpload", forceNoWifiUpload);
        bundle.putString(cmd, FileUploadState.START);
        sendCommand(bundle);
    }

    /**
     * 存储文件信息到数据库
     *
     * @param files
     */
    private void saveFileListToDb(LinkedList<FileInfo> files) {
        final LinkedList<FileInfo> fileList = new LinkedList<FileInfo>();
        final long time = System.currentTimeMillis();

        fileList.addAll(files);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < fileList.size(); i++) {
                    FileInfo item = fileList.get(i);
                    FileUploadDBHelper.getInstance().update(item);
                }
                //Loger.d("chen++++saveFileListToDb()+++time=" + (System.currentTimeMillis() - time));
            }
        }).start();
    }

    /**
     * 向Service发送命令
     *
     * @param bundle
     */
    public void sendCommand(Bundle bundle) {
        Intent intent = new Intent(mContext, FileUploadService.class);
        intent.putExtras(bundle);
        mContext.startService(intent);
    }

    /**
     * 暂停上传
     */
    public void pause() {
        Bundle bundle = new Bundle();
        bundle.putString(cmd, FileUploadState.PAUSE);
        sendCommand(bundle);
    }

    /**
     * 恢复上传
     */
    public void restart() {
        Bundle bundle = new Bundle();
        bundle.putString(cmd, FileUploadState.RESTART);
        sendCommand(bundle);
    }


    /**
     * 停止上传
     */
    public void stop() {
        Bundle bundle = new Bundle();
        bundle.putString(cmd, FileUploadState.STOP);
        sendCommand(bundle);
    }

}
